﻿using AutoMapper;
using CNav.Business.Services;
using CNav.Common;
using CNav.Domain;
using CNav.Model;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CNav.Support;
using WebGrease.Css.Extensions;

namespace CNav.Controllers
{
    [Authorize]
    [AdminAuthorize]
    public class ComplaintTypeController : BaseController
    {
        public ActionResult Index()
        {
            ResponseData<List<ComplaintType>> responseData = Service.Get<IComplaintService>().GetComplaintTypes();
            responseData.ValidateAndThrow();

            var compTypeModels = responseData.Data.Select(Mapper.Map<ComplaintTypeModel>).ToList();
            return View(compTypeModels);
        }

        public ActionResult Details(int id = 0)
        {
            var response = Service.Get<IComplaintService>().GetComplaintType(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<ComplaintTypeModel>(response.Data);

            return View(model);
        }

        public ActionResult Create()
        {
            var complaintTypeModel = new ComplaintTypeModel();
            return View(complaintTypeModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ComplaintTypeModel model)
        {
            if (ModelState.IsValid)
            {
                var complaintTypeDto = Mapper.Map<ComplaintType>(model);

                var response = Service.Get<IComplaintService>().AddComplaintType(complaintTypeDto);
                if (response.Data)
                    return RedirectToAction("Index");

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            return View(model);
        }


        public ActionResult Edit(int id = 0)
        {
            var complaintType = Service.Get<IComplaintService>().GetComplaintType(id);
            if (complaintType == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<ComplaintTypeModel>(complaintType.Data);

            return View(model);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ComplaintTypeModel model)
        {
            if (ModelState.IsValid)
            {
                var complaintTypeDto = Mapper.Map<ComplaintType>(model);
                ResponseData<ComplaintType> response = Service.Get<IComplaintService>().UpdateComplaintType(complaintTypeDto);
                if (response.Error == null)
                    return RedirectToAction("Index");

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            return View(model);
        }

        public ActionResult Delete(int id = 0)
        {
            var complaintType = Service.Get<IComplaintService>().GetComplaintType(id);
            if (complaintType == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<ComplaintTypeModel>(complaintType.Data);

            return View(model);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Service.Get<IComplaintService>().DeleteComplaintType(id);
            return RedirectToAction("Index");
        }
    }
}